
package com.popo.android.activity;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.popo.android.R;

public class MediaPlayerActivity extends Activity implements OnClickListener {
    View play, pause, stop;
    MediaPlayer mediaPlayer = null;
    private AudioManager audioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mediaplay);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        play = findViewById(R.id.play);
        pause = findViewById(R.id.pause);
        stop = findViewById(R.id.stop);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        onClick(play);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.play:
                if (mediaPlayer == null)
                    mediaPlayer = MediaPlayer.create(this, R.raw.back);
                int max = audioManager.getStreamMaxVolume(AudioManager.FLAG_PLAY_SOUND);
                System.out.println("max=" + max);
                for (int i = 0; i < max; i++) {
                    audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 7);
                }
                mediaPlayer.start();
                
                break;
            case R.id.pause:
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                break;
            case R.id.stop:
                if (mediaPlayer != null) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer=null;
                }
                break;
            default:
                break;
        }

    }
    
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }
}
